package com.nanyang.tms.controller.waybill;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import com.nanyang.tms.api.dto.response.waybill.*;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import com.nanyang.common.core.annotation.BindParam;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ServiceException;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.tms.api.dto.request.waybill.WaybillIncomeFeeQueryRequest;
import com.nanyang.tms.api.dto.request.waybill.WaybillPayFeeQueryRequest;
import com.nanyang.tms.api.dto.request.waybill.WaybillPayFeeStateRequest;
import com.nanyang.tms.api.dto.request.waybill.WaybillQueryRequest;
import com.nanyang.tms.api.dto.request.waybill.WaybillTrackQueryRequest;
import com.nanyang.tms.api.service.waybill.WaybillRpcService;

@RestController
@RequestMapping("/waybill")
public class WaybillController {
    @Reference(version = "1.0.0", timeout = 10000, check = false)
    private WaybillRpcService waybillRpcService;

    /**
     * <p>listWaybill</p>
     * <p>Description:查询列表</p>
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResult<PageBean<WaybillPageResponse>> listWaybill(@RequestBody WaybillQueryRequest param) {
        ApiResult<PageBean<WaybillPageResponse>> apiResult = waybillRpcService.listWaybill(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * 轨迹列表
     * @param param
     * @return
     */
    @BindParam
    @RequestMapping(value = "/listTrack", method = RequestMethod.POST)
    public ApiResult<PageBean<WaybillTrackPageResponse>> listWaybillTrack(@BindParam @RequestBody WaybillTrackQueryRequest param) {
        ApiResult<PageBean<WaybillTrackPageResponse>> apiResult = waybillRpcService.listWaybillTrack(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * <p>listWaybill</p>
     * <p>Description:查询根据编码 收货材积重</p>
     *
     * @param waybillCode
     * @return
     */
    @RequestMapping(value = "/listWaybillReceiptVolume/{waybillCode}", method = RequestMethod.GET)
    public ApiResult<List<WaybillReceiptVolumeResponse>> listWaybillReceiptVolume(@PathVariable("waybillCode") String waybillCode) {
        ApiResult<List<WaybillReceiptVolumeResponse>> apiResult = waybillRpcService.listWaybillReceiptVolume(waybillCode);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * <p>listWaybill</p>
     * <p>Description:查询根据编码 出货材积重</p>
     *
     * @param waybillCode
     * @return
     */
    @RequestMapping(value = "/listWaybillShipmentVolume/{waybillCode}", method = RequestMethod.GET)
    public ApiResult<List<WaybillShipmentVolumeResponse>> listWaybillShipmentVolume(@PathVariable("waybillCode") String waybillCode) {
        ApiResult<List<WaybillShipmentVolumeResponse>> apiResult = waybillRpcService.listWaybillShipmentVolume(waybillCode);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * 支出完成
     * @param param
     * @return
     */
    @BindParam
    @RequestMapping(value = "/completeState", method = RequestMethod.POST)
    public ApiResult<Boolean> completeState(@BindParam @RequestBody @Valid WaybillPayFeeStateRequest param) {
        ApiResult<Boolean> apiResult = waybillRpcService.completeState(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * 支付列表
     * @param param
     * @return
     */
    @RequestMapping(value = "/listPayFee", method = RequestMethod.POST)
    public ApiResult<PageBean<WaybillPayFeePageResponse>> listWaybillPayFee(@RequestBody WaybillPayFeeQueryRequest param) {
        ApiResult<PageBean<WaybillPayFeePageResponse>> apiResult = waybillRpcService.listWaybillPayFee(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * 收入列表
     * @param param
     * @return
     */
    @RequestMapping(value = "/listIncomeFee", method = RequestMethod.POST)
    public ApiResult<PageBean<WaybillIncomeFeePageResponse>> listWaybillIncomeFee(@RequestBody WaybillIncomeFeeQueryRequest param) {
        ApiResult<PageBean<WaybillIncomeFeePageResponse>> apiResult = waybillRpcService.listWaybillIncomeFee(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * <p>waybillReceiverSender</p>
     * <p>Description:查询根据编码 收发人</p>
     *
     * @param waybillCode
     * @return
     */
    @RequestMapping(value = "/waybillReceiverSender/{waybillCode}", method = RequestMethod.GET)
    public ApiResult<WaybillReceiverSenderResponse> waybillReceiverSender(@PathVariable("waybillCode") String waybillCode) {
        ApiResult<WaybillReceiverSenderResponse> apiResult = waybillRpcService.waybillReceiverSender(waybillCode);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    @RequestMapping(value ="/notify", method = RequestMethod.POST)
    public ApiResult<Boolean> notify(HttpServletRequest request, @RequestBody String body) {
        ApiResult<Boolean> apiResult = waybillRpcService.notifyTrack(request.getHeader("sign"), body);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * <p>listWaybill</p>
     * <p>Description:查询根据编码 运单详情</p>
     *
     * @param waybillCode
     * @return
     */
    @RequestMapping(value = "/queryDetail/{waybillCode}", method = RequestMethod.GET)
    public ApiResult<WaybillDetailResponse> queryDetail(@PathVariable("waybillCode") String waybillCode) {
        ApiResult<WaybillDetailResponse> apiResult = waybillRpcService.queryDetail(waybillCode);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
}
